package utils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import classifiers.DatasetDescription;

public class Utils {
	
	public static List<String> vals_01; 
	
	static {
		vals_01 = new LinkedList<String>();
		vals_01.add("0");
		vals_01.add("1");
	}
	
	public static String getXML(boolean lvl1,String cluster) {
		if ( lvl1 ) return "mulan_clusters.xml";
		else return "mulan_"+cluster+".xml";
	}
	
	public static double usedMemoryMB() {
		return (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1024.0/1024.0;
	}

	public static String getNameForLevelOne(
			DatasetDescription dd) {
		StringBuilder sb = new StringBuilder("train");
		sb.append("_").append(dd.getNum_instances());
		sb.append("_").append(dd.getFeatures());
		sb.append("_").append(dd.getClassifier());
		for ( String param_value : dd.getParams().getParametersValues() )
			sb.append("_").append(param_value);
		return sb.toString();
	}

	public static String getNameForLevelTwo(
			DatasetDescription dd, TagCluster cluster) {
		StringBuilder sb = new StringBuilder("train");
		sb.append("_").append(dd.getNum_instances());
		sb.append("_").append(cluster.toString());
		sb.append("_").append(dd.getFeatures());
		sb.append("_").append(dd.getClassifier());
		for ( String param_value : dd.getParams().getParametersValues() )
			sb.append("_").append(param_value);
		return sb.toString();
	}
	
	private static final String file_name_split_regex = "_|(\\.txt)";
	
	public static int getNumInstances(String file_name) {
		file_name = getShortFilename(file_name);
		return Integer.parseInt(file_name.split(file_name_split_regex)[1]);
	}
	
	public static String getShortFilename(String file_name) {
		int start_idx = file_name.lastIndexOf('\\');
		if ( start_idx > -1 ) file_name = file_name.substring(start_idx);
		start_idx = file_name.lastIndexOf('/');
		if ( start_idx > -1 ) file_name = file_name.substring(start_idx);
		return file_name;
	}
	
	public static String getCluster(String file_name) {
		file_name = getShortFilename(file_name);
		if ( file_name.split(file_name_split_regex).length < 3 ) return "";
		return file_name.split(file_name_split_regex)[2];
	}
	
}
